﻿package com {
	import com.adobe.serialization.json.JSON;
	import com.data.IData;
	import com.protocols.Mod;
	
	import flash.utils.ByteArray;

	public class Data extends DataBase implements IData {
		private var _callbackList:Object;
		private var _ignoreList:Object;

		public function Data() {
			this._callbackList = {};
			this._ignoreList = {};
			super();
		}

		/**
		 * 初始化数据层
		 * @param	view	视图层引用
		 * @param	ctrol	控制层引用
		 */
		override public function init(view:View, ctrol:Controller):void {
			super.init(view, ctrol);
			_onConnect();
//			this.connect(URI.ip, URI.port);
		}

		override protected function parseSocketData(data:ByteArray):void {
			var modId:uint;
			var funId:uint;
			var id:String;
			var p:Object;
			var p1:Object;
			var buffer:ByteArray = data;
			modId = buffer.readUnsignedByte();
			funId = buffer.readUnsignedByte();

			//trace("modId====" + modId + "funId====" + funId)
			if (modId == 120 && funId == 156) {
				buffer.position = 0;
				buffer.uncompress();
				modId = buffer.readUnsignedByte();
				funId = buffer.readUnsignedByte();
			}

			var term:String = Protocol.concat(modId, funId);
			var callback:Function = this._callbackList[term];

			if (this._ignoreList[term]) {
				callback(buffer);
				dispatch(modId, funId);
				return;
			}
			var result:Array = [];
			for (id in Mod.Modules) {
				if (parseInt(id) == modId) {
					try {
						result = this.analyze(Mod.Modules[id], modId, funId, buffer);
					} catch (e:Error) {
//						_view.alert.confirm("[" + modId + "," + funId + "]" + e);
						p = Protocol.lookupProtocol(modId, funId);
						trace("[response]接口不匹配：", Protocol.getProtocolDescription(p["request"]), "\n", e);
						return;
					}
				}
			}

			trace("[Recv](M:" + modId + " F:" + funId + ") Data:" + JSON.encode(result));

			if (callback is Function) {
				callback(result);
			} else {
				p1 = Protocol.lookupProtocol(modId, funId);
				trace(((("\n\t" + "没有注册接口回调方法：") + Protocol.getProtocolDescription(p1["request"])) + "\n\t------"));
					//throw (new Error(((("\n\t" + "没有注册接口回调方法：") + Protocol.getProtocolDescription(p1["request"])) + "\n\t------")));
			}
			dispatch(modId, funId);
			removeNotAllow(modId, funId);
		}


		/**
		 * 注册数据回调
		 * @param	protocalObj		协议对象
		 * @param	callBack		回调
		 * @param	_arg3
		 */
		public function registerDataCallback(protocalObj:Object, callBack:Function, _arg3:Boolean = false):void {
			var moduleID:uint = protocalObj["module"];
			var actionID:uint = protocalObj["action"];
			var concatID:String = Protocol.concat(moduleID, actionID);
			if (this._callbackList[concatID]) {
				throw(new Error("指定的协议重复注册回调方法！"));
			}
			this._callbackList[concatID] = callBack;
			if (_arg3) {
				this._ignoreList[concatID] = _arg3;
			}
		}

		private function analyze(_arg1:Class, _arg2:uint, _arg3:uint, _arg4:ByteArray):Array {
			var _local9:Object;
			var _local5:Array = [];
			var _local6:Array = _arg1["Actions"];
			var _local7:int = _local6.length;
			var _local8:int;
			while (_local8 < _local7) {
				_local9 = _arg1[_local6[_local8]];
				if (_local9["action"] == _arg3) {
					_local5 = Protocol.analyze(_arg4, _local9["response"]);
					break;
				}
				_local8++;
			}
			return (_local5);
		}

	}

}
